package com.ayz.gateway.api.utils;



import com.ayz.gateway.api.request.ApplicationConfig;
import com.ayz.gateway.api.request.RegistryGatewayServiceProto;
import com.ayz.gateway.common.constants.ApiData;
import com.ayz.gateway.common.constants.GatewayServiceData;
import com.ayz.gateway.common.constants.MethodData;

import java.util.List;

/**
 * @ClassName RequestConvertUtils
 * @Description TODO:完成GRPC请求的Message对象 和 普通POJO对象的转换工具类
 * @Author 快乐的星球
 * @Date 2023/5/26 22:57
 * @Version 1.0
 **/
public class RequestConvertUtils {

    public static RegistryGatewayServiceProto.RegistryGatewayServiceRequest
            generateGrpcRegistryGatewayServiceRequest(GatewayServiceData gatewayServiceData){
        RegistryGatewayServiceProto.RegistryGatewayServiceRequest.Builder builder =
                RegistryGatewayServiceProto.RegistryGatewayServiceRequest.newBuilder();
        builder.setGatewayId(gatewayServiceData.getGatewayId());
        builder.setGatewayName(gatewayServiceData.getGatewayName());
        builder.setAddress(gatewayServiceData.getAddress());
        builder.setGatewayGroup(gatewayServiceData.getGatewayGroup());
        return builder.build();
    }

    public static ApiData toCommonApiData(ApplicationConfig.ApiData apiData){
        ApiData newApiData=new ApiData();
        newApiData.setApplicationName(apiData.getApplicationName());
        newApiData.setInterfaceName(apiData.getInterfaceName());
        newApiData.setTimeout(apiData.getTimeout());
        newApiData.setAsync(apiData.getIsAsync());
        newApiData.setRpcRegistryAddress(apiData.getRpcRegistryAddress());
        //2:组装一个ApiData的方法列表
        List<ApplicationConfig.MethodData> methodDataList = apiData.getMethodDataListList();
        if(!methodDataList.isEmpty()){
            for (ApplicationConfig.MethodData methodData : methodDataList) {
                MethodData newMethodData=new MethodData();
                newMethodData.setMethodName(methodData.getMethodName());
                newMethodData.setAuth(methodData.getIsAuth());
                newMethodData.setUri(methodData.getUri());
                newMethodData.setParameterTypeName(methodData.getParameterTypeName());
                newApiData.addMethodData(newMethodData);
            }
        }
        return newApiData;
    }

    public static ApplicationConfig.ApiData toGrpcApiData(ApiData apiData){
        ApplicationConfig.ApiData.Builder builder = ApplicationConfig.ApiData.newBuilder();
        builder.setApplicationName(apiData.getApplicationName());
        builder.setInterfaceName(apiData.getInterfaceName());
        builder.setTimeout(apiData.getTimeout());
        builder.setIsAsync(apiData.isAsync());
        builder.setRpcRegistryAddress(apiData.getRpcRegistryAddress());
        //组装方法列表
        List<MethodData> methodDataList = apiData.getMethodDataList();
        if(methodDataList!=null && !methodDataList.isEmpty()){
            for (MethodData methodData : methodDataList) {
                ApplicationConfig.MethodData.Builder methodDataBuilder = ApplicationConfig.MethodData.newBuilder();
                methodDataBuilder.setMethodName(methodData.getMethodName());
                methodDataBuilder.setIsAuth(methodData.isAuth());
                methodDataBuilder.setUri(methodData.getUri());
                methodDataBuilder.setParameterTypeName(methodData.getParameterTypeName());
                builder.addMethodDataList(methodDataBuilder.build());
            }
        }
        //返回结果
        return builder.build();
    }
}
